package com.example.mychat.admiservice.controller;


import com.example.groupservice.service.GroupInfoService;
import org.example.annotation.GlobalInteceptor;
import org.example.config.redis.RedisComponent;
import org.example.controller.ABaseController;
import org.example.entity.config.AppConfig;
import org.example.entity.constants.Constants;
import org.example.entity.dto.SysSettingDto;
import org.example.entity.vo.ResponseVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

/**
 * @Description
 * @Author susu
 * @Date 2024/9/8
 */
@RestController
@RequestMapping("/admin")
public class AdminSyssettingController extends ABaseController {

    @Resource
    private GroupInfoService groupInfoService;
    @Resource
    private RedisComponent redisComponent;

    @Resource
    private AppConfig appConfig;


    @RequestMapping("/getSysSetting")
    @GlobalInteceptor(checkAdmin = true)
    public ResponseVO getSysSetting(){
        SysSettingDto sysSetting = redisComponent.getSysSetting();
        return getSuccessResponseVO(sysSetting);
    }

    @RequestMapping("/saveSysSetting")
    @GlobalInteceptor(checkAdmin = true)
    public ResponseVO saveSysSetting(SysSettingDto sysSettingDto, MultipartFile robotFile,MultipartFile robotCover) throws IOException {
        redisComponent.saveSysSetting(sysSettingDto);
        if (robotFile != null) {
            String basePath = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE + Constants.FILE_FOLDER_AVATAR_NAME;
            File baseFile = new File(basePath);
            if (!baseFile.exists()) {
                baseFile.mkdirs();
            }
            String targetPath = baseFile.getPath() + Constants.ROBOT_UID;
            robotFile.transferTo(new File(targetPath + Constants.IMAGE_SUFFIX));
            robotCover.transferTo(new File(targetPath + Constants.COVER_IMAGE_SUFFIX));
        }
        return getSysSetting();
    }
}
